package com.glory.erp.auth.role.service.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.glory.erp.auth.menu.bean.Menu;
import com.glory.erp.auth.resources.bean.Resources;
import com.glory.erp.auth.role.service.RoleService;
import com.glory.erp.auth.role.dao.RoleDao;
import com.glory.erp.auth.role.bean.Role;
import com.glory.erp.auth.base.BaseQueryModel;

public class RoleServiceImpl implements RoleService {

	private RoleDao roleDao;

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	// 废弃
	public void save(Role role) {
	}

	// 废弃
	public void update(Role role) {
	}

	public void delete(Role role) {
		roleDao.delete(role);
	}

	public Role findById(Serializable uuid) {
		return roleDao.findById(uuid);
	}

	public List<Role> findAll() {
		return roleDao.findAll();
	}

	public List<Role> findAll(BaseQueryModel qm, Integer pageNum,
			Integer pageCount) {
		return roleDao.findAll(qm, pageNum, pageCount);
	}

	public Integer getCount(BaseQueryModel qm) {
		return roleDao.getCount(qm);
	}

	public void save(Role role, Long[] resourcesUuids) {
		// array --> set
		Set<Resources> res = new HashSet<Resources>();
		for (Long uuid : resourcesUuids) {
			Resources r = new Resources();
			r.setUuid(uuid);
			res.add(r);
		}
		role.setResources(res);
		roleDao.save(role);
	}

	public void update(Role role, Long[] resourcesUuids) {
		Set<Resources> res = new HashSet<Resources>();
		for (Long uuid : resourcesUuids) {
			Resources r = new Resources();
			r.setUuid(uuid);
			res.add(r);
		}
		role.setResources(res);
		roleDao.update(role);
	}

	public void save(Role role, Long[] resourcesUuids, Long[] menuUuids) {
		// 添加角色 同时添加资源
		Set<Resources> res = new HashSet<Resources>();
		for (Long uuid : resourcesUuids) {
			Resources r = new Resources();
			r.setUuid(uuid);
			res.add(r);
		}
		role.setResources(res);
		// 添加菜单
		Set<Menu> menu = new HashSet<Menu>();
		for (Long uuid : menuUuids) {
			Menu m = new Menu();
			m.setUuid(uuid);
			menu.add(m);
		}
		role.setMenus(menu);
		roleDao.save(role);
	}

	public void update(Role role, Long[] resourcesUuids, Long[] menuUuids) {
		// 修改角色 修改资源
		Set<Resources> res = new HashSet<Resources>();
		for (Long uuid : resourcesUuids) {
			Resources r = new Resources();
			r.setUuid(uuid);
			res.add(r);
		}
		role.setResources(res);
		// 修改菜单
		Set<Menu> menu = new HashSet<Menu>();
		for(Long uuid:menuUuids){
			Menu m = new Menu();
			m.setUuid(uuid);
			menu.add(m);
		}
		role.setMenus(menu);
		roleDao.update(role);
	}
}